package com.luocj.looook.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luocj.looook.R;
import com.luocj.looook.activity.DarwerActivty;
import com.luocj.looook.adapter.MultipleItemAdapter;
import com.luocj.looook.adapter.MultipleItemLAdapter;
import com.luocj.looook.base.BaseFragment;
import com.luocj.looook.model.ModelTest;
import com.luocj.looook.model.MultipeItemL;
import com.luocj.looook.model.MultipleItem;
import com.luocj.looook.utils.BaseDecoration;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luocj515 on 2017/12/4.
 * 项目名称：LooooK
 * 类描述：
 * 创建人：hasee
 * 创建时间：2017/12/4 18:03
 * 修改备注：
 */

public class DrawerFragment extends BaseFragment {

    private int[] images = new int[]{R.mipmap.ic_launcher, R.mipmap.b1, R.mipmap.b2, R.mipmap.b3};
    private ArrayList<ModelTest> modelTests;
    private SmartRefreshLayout smartFreshLayout;
    private RecyclerView recyclerView;
    private List<MultipleItem> data;
    private MultipleItemAdapter multipleItemAdapter;
    private ArrayList<MultipeItemL> multipeItemLS;
    private MultipleItemLAdapter myAdapter;
    private AlertDialog mDialog = null;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_drawer;
    }

    @Override
    protected void initData() {
        modelTests = new ArrayList<ModelTest>();
        for (int i = 0; i < 10; i++) {
            ModelTest modelTest = new ModelTest(images[i % 4], "title" + i, "content" + i, (int) (Math.random() * 2) + 1);
            modelTests.add(modelTest);
        }

        multipeItemLS = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            MultipeItemL multipeItemL = new MultipeItemL(1, 1);
            MultipeItemL multipeItemL1 = new MultipeItemL(2, 3);
            MultipeItemL multipeItemL2 = new MultipeItemL(3, 4);
            multipeItemLS.add(multipeItemL);
            multipeItemLS.add(multipeItemL1);
            multipeItemLS.add(multipeItemL2);

        }
        for (int i = 0; i < multipeItemLS.size(); i++) {
            Logger.i(multipeItemLS.get(i).toString() + "--------------->" + i);
        }
    }

    @Override
    protected void initView() {
        smartFreshLayout = mContentView.findViewById(R.id.smartrefreshlayout_drawer);
        recyclerView = mContentView.findViewById(R.id.recyclerview_drawer);
//        data = DataServer.getMultipleItemData();
//        multipleItemAdapter = new MultipleItemAdapter(getActivity(), data);
//        final GridLayoutManager manager = new GridLayoutManager(getActivity(), 4);
//        recyclerView.setLayoutManager(manager);
//        multipleItemAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
//                return data.get(position).getSpanSize();
//            }
//        });
//        recyclerView.setAdapter(multipleItemAdapter);
//        Logger.i(multipleItemAdapter.getItemCount() + "");

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        myAdapter = new MultipleItemLAdapter(getActivity(), multipeItemLS);
        myAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                return multipeItemLS.get(position).getSpanSize();
            }
        });
//        DividerItemDecoration itemDecoration = new DividerItemDecoration();
//        recyclerView.addItemDecoration(itemDecoration);

        recyclerView.addItemDecoration(new BaseDecoration(Color.RED, 10));
        recyclerView.setAdapter(myAdapter);

        myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position == 0) {

                    if (mDialog == null) {
                        mDialog = new AlertDialog.Builder(getActivity()).create();
                    }
                    mDialog.show();
                    final Window window = mDialog.getWindow();
                    if (window != null) {
                        window.setContentView(R.layout.dialog_pay_panel);
                        window.setGravity(Gravity.BOTTOM);
                        window.setWindowAnimations(R.style.anim_panel_up_from_bottom);
                        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        //设置属性
                        final WindowManager.LayoutParams params = window.getAttributes();
                        params.width = WindowManager.LayoutParams.MATCH_PARENT;
                        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                        window.setAttributes(params);

                        window.findViewById(R.id.btn_dialog_pay_alpay).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getContext(), "0", Toast.LENGTH_SHORT).show();
                            }
                        });
                        window.findViewById(R.id.btn_dialog_pay_wechat).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getContext(), "1", Toast.LENGTH_SHORT).show();

                            }
                        });
                        window.findViewById(R.id.btn_dialog_pay_cancel).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                Toast.makeText(getContext(), "2", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }
            }
        });

//        smartFreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
//            @Override
//            public void onLoadmore(RefreshLayout refreshlayout) {
//                List<MultipleItem> multipleItemData = DataServer.getMultipleItemData();
//                multipleItemAdapter.addData(multipleItemData);
//                smartFreshLayout.finishLoadmore();
//                Logger.i(multipleItemAdapter.getItemCount() + "");
//            }
//
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                refreshlayout.getLayout().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        data.clear();
//                        multipleItemAdapter.notifyDataSetChanged();
//                        smartFreshLayout.finishRefresh();
//                        smartFreshLayout.setLoadmoreFinished(false);
//                        Logger.i(multipleItemAdapter.getItemCount() + "");
//                    }
//                }, 2000);
//            }
//        });

        mContentView.findViewById(R.id.btn_drawer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivity(new Intent(getActivity(), DarwerActivty.class));
            }
        });
    }

}
